import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemoHash {
    public static void  test1(Jedis jedis) {
        System.out.println("hset hget");
        jedis.flushAll();

        jedis.hset("key", "f1", "111");
        Map<String , String> map = new HashMap<>();
        map.put("f2", "222");
        map.put("f3", "333");
        map.put("f4", "444");
        jedis.hset("key", map);
        Set<String> val = jedis.hkeys("key");
        System.out.println(val);

        List<String> v = jedis.hmget("key", "f1", "f2", "f3");

        System.out.println(jedis.hget("key","f1"));
        System.out.println(jedis.hmget("key","f1", "f2", "f3"));

    }

    public static void  test2(Jedis jedis) {
        System.out.println("hexists");
        jedis.flushAll();

        jedis.hset("key", "f1", "111");
        Map<String , String> map = new HashMap<>();
        map.put("f2", "222");
        map.put("f3", "333");
        map.put("f4", "444");
        jedis.hset("key", map);

        boolean res = jedis.hexists("key", "f1");
        System.out.println(res);

        res = jedis.hexists("key", "f100");
        System.out.println(res);

    }

    public static void  test3(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushAll();

        jedis.hset("key", "f1", "111");
        Map<String , String> map = new HashMap<>();
        map.put("f2", "222");
        map.put("f3", "333");
        map.put("f4", "444");
        jedis.hset("key", map);

        Boolean res = jedis.hexists("key", "f100");
        System.out.println(res);

        long i = jedis.hdel("key", "f2");
        System.out.println(i);

        res = jedis.hexists("key", "f2");
        System.out.println(res);
    }

    public static void  test4(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushAll();

        jedis.hset("key", "f1", "111");
        Map<String , String> map = new HashMap<>();
        map.put("f2", "222");
        map.put("f3", "333");
        map.put("f4", "444");
        jedis.hset("key", map);

        Set<String> keys = jedis.hkeys("key");
        List<String> values = jedis.hvals("key");
        System.out.println(keys);
        System.out.println(values);
    }

    public static void  test5(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushAll();

        Map<String , String> map = new HashMap<>();
        map.put("f1", "111");
        map.put("f2", "222");
        map.put("f3", "333");
        map.put("f4", "444");
        String res = jedis.hmset("key", map);
        System.out.println(res);
        List<String> hmget = jedis.hmget("key", "f1", "f2", "f3", "f4", "f5");
        System.out.println(hmget);

    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()){
            test5(jedis);
        }
    }
}
